"""类型约束系统演示

展示如何使用 TypeVar 约束来定义更精确的函数签名
"""

from typing import TypeVar
from typing_extensions import Integer, Number, Comparable
from mc.types import int32_t, uint64_t, size_t

# 定义约束类型变量
T = TypeVar("T")
IntegerType = TypeVar("IntegerType", bound=Integer)
NumberType = TypeVar("NumberType", bound=Number)
ComparableType = TypeVar("ComparableType", bound=Comparable)


def add_integers(a: IntegerType, b: IntegerType) -> IntegerType:
    """只接受整数类型的加法函数"""
    return a + b


def max_value(a: ComparableType, b: ComparableType) -> ComparableType:
    """返回两个可比较值中的最大值"""
    return a if a > b else b


def sum_numbers(values: list[NumberType]) -> NumberType:
    """计算数值列表的和"""
    result = values[0]
    for i in range(1, len(values)):
        result += values[i]
    return result


def test_type_constraints():
    """测试类型约束"""
    # 这些调用应该都是合法的
    result1 = add_integers(42, 100)  # int
    result2 = add_integers(int32_t(10), int32_t(20))  # int32_t
    result3 = add_integers(uint64_t(5), uint64_t(15))  # uint64_t

    max1 = max_value(1, 2)  # int
    max2 = max_value(3.14, 2.71)  # float
    max3 = max_value("hello", "world")  # str

    sum1 = sum_numbers([1, 2, 3, 4, 5])  # list[int]
    sum2 = sum_numbers([1.1, 2.2, 3.3])  # list[float]

    return result1, result2, result3, max1, max2, max3, sum1, sum2


if __name__ == "__main__":
    test_type_constraints()
